<template>
	<el-main>
		<el-row :gutter="15">
			<el-col :xl="6" :lg="6" :md="8" :sm="12" :xs="24" v-for="item in list" :key="item.id">
				<el-card class="task task-item" shadow="hover">
					<h2>{{item.eventName}}</h2>
					<ul>
						<li>
							<h4>创建时间</h4>
							<p>{{item.createTime}}</p>
						</li>
						<li>
							<h4>跳转链接</h4>
							<p>{{item.jumpLink}}</p>
						</li>
					</ul>
					<div class="bottom">
						<div class="state">
							<el-tag v-if="item.status===1" size="small">已启用</el-tag>
							<el-tag v-if="item.status===2" size="small" type="info">已停用</el-tag>
						</div>
						<div class="handler">
							<el-popconfirm title="确定更新吗？" @confirm="run(item)">
								<template #reference>
									<el-button type="primary" icon="el-icon-caret-right" circle></el-button>
								</template>
							</el-popconfirm>
							<el-dropdown trigger="click">
								<el-button type="primary" icon="el-icon-more" circle plain></el-button>
								<template #dropdown>
									<el-dropdown-menu>
										<el-dropdown-item @click="edit(item)">编辑</el-dropdown-item>
										<el-dropdown-item @click.stop="del(item)" divided>删除</el-dropdown-item>
									</el-dropdown-menu>
								</template>
							</el-dropdown>
						</div>
					</div>
				</el-card>
			</el-col>
			<el-col :xl="6" :lg="6" :md="8" :sm="12" :xs="24">
				<el-card class="task task-add" shadow="none" @click="add">
					<el-icon>
						<el-icon-plus/>
					</el-icon>
					<p>新增模块</p>
				</el-card>
			</el-col>
		</el-row>
	</el-main>

	<save-dialog v-if="dialog.save" ref="saveDialog" @success="handleSuccess" @closed="dialog.save=false"></save-dialog>

	<el-drawer title="计划任务日志" v-model="dialog.logsVisible" :size="600" direction="rtl" destroy-on-close>
		<logs></logs>
	</el-drawer>
</template>

<script>
	import saveDialog from './save'
	import logs from './logs'

	export default {
		name: 'task',
		components: {
			saveDialog,
			logs
		},
		provide() {
			return {
				list: this.list
			}
		},
		data() {
			return {
				dialog: {
					save: false,
					logsVisible: false
				},
				list: [],
				params: {
					page: 1,
					pageSize: 20
				}
			}
		},
		mounted() {
			this.getList()
		},
		methods: {
			getList() {
				this.$API.operate.event.list.post(this.params).then(res => {
					this.list = res.data.records
				})
			},
			add() {
				this.dialog.save = true
				this.$nextTick(() => {
					this.$refs.saveDialog.open()
				})
			},
			edit(task) {
				this.dialog.save = true
				this.$nextTick(() => {
					this.$refs.saveDialog.open('edit').setData(task)
				})
			},
			del(task) {
				this.$confirm(`确认删除【${task.eventName}】模块吗？`, '提示', {
					type: 'warning',
					confirmButtonText: '删除',
					cancelButtonText: '取消',
					confirmButtonClass: 'el-button--danger'
				}).then(() => {
					setTimeout(()=>{
						const loading = this.$loading()
						loading.close()
						this.$API.operate.event.delete(task.id)
					},1000)
				}).catch(() => {
				});
			},
			logs() {
				this.dialog.logsVisible = true
			},
			run(task) {
				this.$API.operate.event.update.post({
					id: task.id,
					status: task.status === 1 ? 2 : 1
				}).then(res => {
					if (res.success) {
						this.$message.success(`已${task.status === 1 ? '停用' : '启用'}成功：${task.eventName}`)
						this.getList()
					}
				})

			},
			//本地更新数据
			handleSuccess(data, mode) {
				if (mode == 'add') {
					data.id = new Date().getTime()
					this.list.push(data)
				} else if (mode == 'edit') {
					this.list.filter(item => item.id === data.id).forEach(item => {
						Object.assign(item, data)
					})
				}
			}
		}
	}
</script>

<style scoped>
	.task {
		height: 210px;
	}

	.task-item h2 {
		font-size: 15px;
		color: #3c4a54;
		padding-bottom: 15px;
	}

	.task-item li {
		list-style-type: none;
		margin-bottom: 10px;
	}

	.task-item li h4 {
		font-size: 12px;
		font-weight: normal;
		color: #999;
	}

	.task-item li p {
		margin-top: 5px;
	}

	.task-item .bottom {
		border-top: 1px solid #EBEEF5;
		text-align: right;
		padding-top: 10px;
		display: flex;
		justify-content: space-between;
		align-items: center;
	}

	.task-add {
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;
		text-align: center;
		cursor: pointer;
		color: #999;
	}

	.task-add:hover {
		color: #409EFF;
	}

	.task-add i {
		font-size: 30px;
	}

	.task-add p {
		font-size: 12px;
		margin-top: 20px;
	}
</style>
